﻿#include "BrowserTableHandlerPhysicsRibbed.h"
#include "OperatorsInterface/OperAbstractPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyRibbed.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyRibInfo.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include <QPushButton>

namespace GUI
{
    void BrowserTableHandlerPhysicsRibbed::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Skin")
            << tr("1-Dir,Rib") << tr("1-Dir.Spacing Mean[m]") << tr("1-Dir.Spacing Std.Dev.[%]") << tr("1-Dir.Heinght[m]")
            << tr("2-Dir,Rib") << tr("2-Dir.Spacing Mean[m]") << tr("2-Dir.Spacing Std.Dev.[%]") << tr("2-Dir.Heinght[m]")
            << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }
    void BrowserTableHandlerPhysicsRibbed::updateData()
    {
        if (_tableWidget == nullptr) return;
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysicsPropManager* mgr = phys->getPhysicsPropManager();
        if (mgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> list = mgr->getPhysicsProps(
            Acoustics::FITKAcousticsAbsPhysicsProp::APPRibbedphysicalProperty);
        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为加筋板
            Acoustics::FITKPhyscisPropertyRibbed* prop = dynamic_cast<Acoustics::FITKPhyscisPropertyRibbed*>(list.at(i));
            if (prop == nullptr) continue;
            Acoustics::FITKPhyscisPropertyRibInfo* ribInfo1 = prop->getDri1RibInfo();
            if(ribInfo1 == nullptr) continue;
            //数据ID
            const int id = prop->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(prop->getDataObjectName(), id));
            auto skin = prop->getSkinPropObj();
            if(skin)
                _tableWidget->setItem(i, 1, new QTableWidgetItem(skin->getDataObjectName(), id));
            //设置第一条筋
            auto beam1 = ribInfo1->getBeamPropObj();
            if(beam1)
                _tableWidget->setItem(i, 2, new QTableWidgetItem(beam1->getDataObjectName(), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(ribInfo1->getSpacing()), id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(ribInfo1->getSpacingDevFraction()*100), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(ribInfo1->getOffset()), id));
            //设置第二条筋
            Acoustics::FITKPhyscisPropertyRibInfo* ribInfo2 = prop->getDri2RibInfo();
            if (ribInfo2 == nullptr) continue;
            auto beam2 = ribInfo2->getBeamPropObj();
            if (beam2)
                _tableWidget->setItem(i, 6, new QTableWidgetItem(beam2->getDataObjectName(), id));
            else
                _tableWidget->setItem(i, 6, new QTableWidgetItem("None", id));
            _tableWidget->setItem(i, 7, new QTableWidgetItem(QString::number(ribInfo2->getSpacing()), id));
            _tableWidget->setItem(i, 8, new QTableWidgetItem(QString::number(ribInfo2->getSpacingDevFraction() * 100), id));
            _tableWidget->setItem(i, 9, new QTableWidgetItem(QString::number(ribInfo2->getOffset()), id));
            float color[3] = { 0,0,0 };
            prop->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 10, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, prop); });
            _tableWidget->setItem(i, 11, new QTableWidgetItem(prop->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }
    EventOper::OperAbstractPhysics * BrowserTableHandlerPhysicsRibbed::getPhysicsPropOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractPhysics>("OperPhysicsPropRibbed");
    }
    bool BrowserTableHandlerPhysicsRibbed::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //
        if (colIndex == 0|| colIndex == 2|| colIndex == 5|| colIndex == 6|| colIndex == 9|| colIndex == 10|| colIndex == 11) return false;

        return true;
    }
}

